import router, { resetRouter } from './router'

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { Route } from 'vue-router'
import { UserModule } from '@/store/modules/user'
import { PermissionModule } from '@/store/modules/permission'
import Msger from '@/utils/msg'
// import i18n from '@/lang' // Internationalization

NProgress.configure({ showSpinner: false })

const whiteList = ['/login', '/auth-redirect']

const getPageTitle = (key: string) => {
  // const hasKey = i18n.te(`route.${key}`)
  // if (hasKey) {
  //   const pageName = i18n.t(`route.${key}`)
  //   return `${pageName} - ${settings.title}`
  // }
  return (key && `Lazypg Admin | ${key}`) || 'Lazypg Admin'
}
router.beforeEach(async(to: Route, _: Route, next: any) => {
  console.log('/登录拦截', UserModule.userInfo)
  // Start progress bar
  NProgress.start()
  PermissionModule.INIT_LOCAL_ROUTES()

  if (
    UserModule.userInfo.sid ||
    localStorage.getItem('sid') ||
    whiteList.includes(to.path)
  ) {
    console.log('1111', UserModule.userInfo)
    next()
    NProgress.done()
  } else {
    next()
    NProgress.done()
    // UserModule.LogOut()
    // next(`/login?redirect=${to.path}`)
    // NProgress.done()
  }

  // if (whiteList.includes(to.path)) {
  //   next()
  //   NProgress.done()
  //   return
  // }
})

router.afterEach((to: Route) => {
  // Finish progress bar
  // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939
  NProgress.done()

  // set page title
  document.title = getPageTitle((to as any).meta.title)
})
